package com.kucai.yifaqapp.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.NotecountDetail;
import com.kucai.yifaqapp.model.network.entity.res.NotecountDetailBean;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.PriceUtils;
import com.kucai.yifaqapp.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.kucai.yifaqapp.global.App.getContext;

/**
 * Created by admin on 2018/12/9.
 */

public class NoteActivitySecond extends BaseActivitySimple {
    private LinearLayout skinbg;
    private RecyclerView notelist;
    private Adapter adapter;
    private Adapter1 adapter1;
    private int gameid = -1;
    private String gamename;
    private NotecountDetailBean beans;
    private NotecountDetail beans1;
    private TextView headerTitle;
    private TextView mingxititle;
    private LinearLayout goback;
    private LinearLayout mingxilin;
    private String statDate;
    private TextView numwin;
    private TextView zongshuying;
    private TextView numxiazhu;

    @Override
    protected int setContentView() {
        return R.layout.activity_notesecond;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameid = getIntent().getIntExtra("gameid", -1);
        gamename = getIntent().getStringExtra("gamename");
        statDate = getIntent().getStringExtra("statDate");
        skinbg = findViewById(R.id.skinbg);
        goback = findViewById(R.id.goback);
        mingxititle = findViewById(R.id.mingxititle);
        mingxilin = findViewById(R.id.mingxilin);
        numwin = findViewById(R.id.numwin);
        zongshuying = findViewById(R.id.zongshuying);
        numxiazhu = findViewById(R.id.numxiazhu);
        notelist = findViewById(R.id.notelist);
        headerTitle = findViewById(R.id.headerTitle);
        if (!TextUtils.isEmpty(gamename)) {
            mingxilin.setVisibility(View.VISIBLE);
            mingxititle.setText(gamename);
            headerTitle.setText("即时注单");
            initData();
        } else {
            mingxilin.setVisibility(View.GONE);
            headerTitle.setText("下注明细");
            initData1();
        }

        goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        notelist.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter = new Adapter();
        adapter1 = new Adapter1();
    }

    public String formateRate(String rateStr) {
        if (rateStr.indexOf(".") != -1) {
            //获取小数点的位置
            int num = 0;
            //找到小数点在字符串中的位置,找到返回一个int类型的数字,不存在的话返回 -1
            num = rateStr.indexOf(".");

            String dianAfter = rateStr.substring(0, num + 1);//输入100.30,dianAfter = 100.
            String afterData = rateStr.replace(dianAfter, "");//把原字符(rateStr)串包括小数点和小数点前的字符替换成"",最后得到小数点后的字符(不包括小数点)

            //判断小数点后字符的长度并做不同的操作,得到小数点后两位的字符串
            if (afterData.length() < 2) {
                afterData = afterData + "0";
            } else {
                afterData = afterData;
            }
            //返回元字符串开始到小数点的位置 + "." + 小数点后两位字符
            return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
        } else {
            if (rateStr == "1") {
                return "100";
            } else {
                return rateStr;
            }
        }
    }

    private void initData() {
        HttpJsonUtils.getHttpApiInstance()
                .get_note_count(HttpAdress.getuseAdress() + "api/mobile/lottery/getNotcountDetail", Info.user.token, gameid, 100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<NotecountDetail>() {
                    @Override
                    protected void onCustomNext(NotecountDetail resBanner) {
                        if (resBanner.getData() != null) {
                            beans1 = resBanner;
                            notelist.setAdapter(adapter);
//                            numwin.setText(formateRate(resBanner.getOtherData().getTotalResultMoney() + ""));
                            zongshuying.setText("");
                            numwin.setVisibility(View.INVISIBLE);
                            numxiazhu.setText(formateRate(resBanner.getOtherData().getTotalBetMoney() + ""));
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void initData1() {
        HttpJsonUtils.getHttpApiInstance()
                .getbets(HttpAdress.getuseAdress() + "api/mobile/user/getUserBets", Info.user.token, gameid, statDate, "100")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<NotecountDetailBean>() {
                    @Override
                    protected void onCustomNext(NotecountDetailBean resBanner) {
                        if (resBanner.getData() != null) {
                            beans = resBanner;
                            notelist.setAdapter(adapter1);
                            zongshuying.setVisibility(View.VISIBLE);
                            numwin.setVisibility(View.VISIBLE);
                            numwin.setText(formateRate(resBanner.getOtherData().getTotalResultMoney() + ""));
                            numxiazhu.setText(formateRate(resBanner.getOtherData().getTotalBetMoney() + ""));
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });


    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.note_itemsecond, parent, false));

        }

        @Override
        public void onBindViewHolder(final Adapter.MViewHolder holder, final int position) {
            holder.text1.setText(beans1.getData().get(position).getTurnNum() + "");
            Double a = beans1.getData().get(position).getRebate() * 100;
            holder.text2.setText(beans1.getData().get(position).getPlayCateName() + beans1.getData().get(position).getPlayName()
                    + "\n" + "赔率：" + beans1.getData().get(position).getOdds() + "\n" + "退水：" + a + "%");

            holder.text3.setText(beans1.getData().get(position).getMoney() + "");
            PriceUtils.format(holder.text3);
            if (beans1.getData().get(position).getResultMoney() > 0) {
                holder.text4.setText(beans1.getData().get(position).getResultMoney() + "");
                holder.text4.setTextColor(getContext().getResources().getColor(R.color.orangeRed));
            } else {
                holder.text4.setText(beans1.getData().get(position).getResultMoney() + "");
                holder.text4.setTextColor(getContext().getResources().getColor(R.color.Green));
            }
            PriceUtils.format(holder.text4);
        }

        @Override
        public int getItemCount() {
            return beans1.getData().size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView text1, text2, text3, text4;
            LinearLayout notelinitem;

            MViewHolder(View itemView) {
                super(itemView);
                notelinitem = (LinearLayout) itemView.findViewById(R.id.notelinitem);
                text1 = (TextView) itemView.findViewById(R.id.text1);
                text2 = (TextView) itemView.findViewById(R.id.text2);
                text3 = (TextView) itemView.findViewById(R.id.text3);
                text4 = (TextView) itemView.findViewById(R.id.text4);
            }
        }
    }

    class Adapter1 extends RecyclerView.Adapter<Adapter1.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter1.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter1.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.note_itemsecond, parent, false));

        }


        @Override
        public void onBindViewHolder(final Adapter1.MViewHolder holder, final int position) {
            holder.text1.setText(beans.getData().get(position).getTurnNum() + "");
            long a = beans.getData().get(position).getRebate() * 100;

            holder.text2.setText(beans.getData().get(position).getPlayCateName() + beans.getData().get(position).getPlayName()
                    + "\n" + "赔率：" + beans.getData().get(position).getOdds() + "\n" + "退水：" + a + "%");

            holder.text3.setText(beans.getData().get(position).getMoney() + "");
            PriceUtils.format(holder.text3);
            if (beans.getData().get(position).getResultMoney() > 0) {
                holder.text4.setText(beans.getData().get(position).getResultMoney() + "");
                holder.text4.setTextColor(getContext().getResources().getColor(R.color.orangeRed));
            } else {
                holder.text4.setText(beans.getData().get(position).getResultMoney() + "");
                holder.text4.setTextColor(getContext().getResources().getColor(R.color.Green));
            }
            PriceUtils.format(holder.text4);
        }

        @Override
        public int getItemCount() {
            return beans.getData().size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView text1, text2, text3, text4;

            MViewHolder(View itemView) {
                super(itemView);
                text1 = (TextView) itemView.findViewById(R.id.text1);
                text2 = (TextView) itemView.findViewById(R.id.text2);
                text3 = (TextView) itemView.findViewById(R.id.text3);
                text4 = (TextView) itemView.findViewById(R.id.text4);
            }
        }
    }
}
